package com.yfp.client.util;

import lombok.Data;

import java.io.Serializable;

@Data
public class Response<T> implements Serializable {

    private Integer code;
    private T data;
    private String msg;

    public Response() {

    }

    public static <T> Response<T> success(T data) {
        Response<T> apiResult = new Response<>();
        apiResult.setCode(ResponseEnum.OPERATOR_SUCCESS.getCode());
        apiResult.setData(data);
        apiResult.setMsg(ResponseEnum.OPERATOR_SUCCESS.getMsg());
        return apiResult;
    }

    public static <T> Response<T> success() {
        return result(ResponseEnum.OPERATOR_SUCCESS);
    }

    public static <T> Response<T> failuer() {
        return result(ResponseEnum.OPERATOR_FAILUER);
    }

    public static <T> Response<T> failuer(T data) {
        return result(ResponseEnum.OPERATOR_FAILUER, data);
    }

    public static <T> Response<T> result(ResponseEnum responseEnum, T data) {
        return restResult(data, responseEnum.getCode(), responseEnum.getMsg());
    }

    public static <T> Response<T> result(ResponseEnum responseEnum) {
        return restResult(null, responseEnum.getCode(), responseEnum.getMsg());
    }

    private static <T> Response<T> restResult(T data, int code, String msg) {
        Response<T> apiResult = new Response<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }
}
